package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.GoodsSpecGroupDAO;
import com.ecoolex.chinadutyfree.retailer.entity.Goods;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsSpecGroup;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsSpecGroup.Column;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsSpecGroupExample;
import com.ecoolex.chinadutyfree.retailer.pojo.EditSpecGroupDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;


@Service
public class GoodsSpecGroupManager {

	@Autowired
	private GoodsSpecGroupDAO dao;

	@Autowired
	private GoodsSpecManager goodsSpecManager;

	private void verify(EditSpecGroupDTO group) {
		BizAssert.isTrue(Check.notNullOrTrimEmpty(group.getName()), ResultCode.VERIFY.build(31), "规格分组名称为空");
		BizAssert.isTrue(Check.notNullOrEmpty(group.getSpecList()), ResultCode.VERIFY.build(32), "规格分组下至少需要一个规格值");
	}

	public void update(Goods goods, List<EditSpecGroupDTO> specGroup, String updateUser) {
		List<GoodsSpecGroup> oldList = queryByGoodsId(goods.getGoodsId());
		for (int i = 0, size = specGroup.size(); i < size; i++) {
			EditSpecGroupDTO input = specGroup.get(i);
			verify(input);

			if (Check.notNull(input.getSpecGroupId())) {
				GoodsSpecGroup old = oldList.stream().collect(
					CollectorUtil.first(g -> input.getSpecGroupId().equals(g.getSpecGroupId())));
				BizAssert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(30), "规格分组={}不存在", input.getSpecGroupId());

				old.setName(input.getName());
				old.setSorting(i + 1);
				old.setUpdateUser(updateUser);
				old.setUpdateTime(Date.from(Instant.now()));
				dao.updateByPrimaryKeySelective(old,
					Column.excludes(Column.goodsId, Column.deleted, Column.createUser, Column.createTime));
				// 移除
				oldList.remove(old);
				// 更新规格值
				goodsSpecManager.update(old, input.getSpecList(), updateUser);
			} else {
				GoodsSpecGroup group = new GoodsSpecGroup();
				group.setGoodsId(goods.getGoodsId());
				group.setName(input.getName());
				group.setSorting(i + 1);
				group.setDeleted(GoodsSpecGroup.NOT_DELETED);
				group.setCreateUser(updateUser);
				group.setCreateTime(Date.from(Instant.now()));
				group.setUpdateUser(updateUser);
				group.setUpdateTime(Date.from(Instant.now()));
				dao.insert(group);
				// 设置输入groupId, 后面规格需要
				input.setSpecGroupId(group.getSpecGroupId());
				// 更新规格值
				goodsSpecManager.update(group, input.getSpecList(), updateUser);
			}
		}
		// 删除多余的规格分组
		oldList.forEach(old -> delete(old, updateUser));
	}

	public List<GoodsSpecGroup> queryByGoodsId(Integer goodsId) {
		GoodsSpecGroupExample example = new GoodsSpecGroupExample();
		example.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(GoodsSpecGroup.NOT_DELETED);
		// 排序
		example.orderBy(Column.sorting.asc());
		return dao.selectByExample(example);
	}

	public void deleteByGoodsId(Integer goodsId, String updateUser) {
		List<GoodsSpecGroup> groupList = queryByGoodsId(goodsId);

		groupList.forEach(group -> delete(group, updateUser));
	}

	private void delete(GoodsSpecGroup group, String updateUser) {
		group.setDeleted(GoodsSpecGroup.IS_DELETED);
		group.setUpdateUser(updateUser);
		group.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(group, Column.deleted, Column.updateUser, Column.updateTime);

		// 删除规格值
		goodsSpecManager.deleteByGroupId(group.getSpecGroupId(), updateUser);
	}
}